<template>
  <div class="user-container">
    <!-- 头部 -->
    <div class="info-box">
      <van-image
        round
        fit="cover"
        width="64"
        height="64"
        :src="userinfo.photo"
      />
      <h3>
        {{ userinfo.name }}<br />
        <van-tag color="#fff" text-color="#0098fa">{{
          userinfo.birthday
        }}</van-tag>
      </h3>
    </div>
    <!-- 中间列表 -->
    <van-row>
      <van-col span="8">
        <van-icon name="newspaper-o" color="#5facff" size="26" />
        <span>我的作品</span>
      </van-col>
      <van-col span="8">
        <van-icon name="star-o" color="#ff007a" size="26" />
        <span>我的收藏</span>
      </van-col>
      <van-col span="8">
        <van-icon name="tosend" color="#ffa600" size="26" />
        <span>阅读历史</span>
      </van-col>
    </van-row>
    <!-- ceil单元格布局 -->
    <van-cell-group>
      <van-cell title="编辑资料" icon="edit" is-link @click="$router.push('/edit')"/>
      <van-cell title="小智同学" icon="chat-o" is-link @click="$router.push('/student')"/>
      <van-cell title="系统设置" icon="setting-o" is-link />
      <van-cell title="退出登录" icon="warning-o" is-link @click="logoutClick" />
    </van-cell-group>
  </div>
</template>

<script>
// 导入获取信息接口
import { getUserInfo } from './user.js'
import {removeToken} from '@/utils/token.js'
export default {
  name: 'user',
  data () {
    return {
      userinfo: {} //保存数据
    }
  },
  methods: {
    logoutClick () {
      this.$dialog
        .confirm({
          title: '温馨提示',
          message: '您确定退出吗',
          theme: 'round-button',
        })
        .then(() => {
          // 清空token 和 数据,并跳转页面
          removeToken()
          this.userinfo = {}
          this.$router.push({ path: '/login' })
        })
        .catch(() => {
          // on cancel
          console.log('catch');
        })
    }
  },
  created () {
    getUserInfo().then(res => {
      // console.log(res)
      this.userinfo = res.data.data
      // 将数据保存到 vuex 里的 state 中
      this.$store.commit('setUserinfo', res.data.data)
    })
  }
}
</script>

<style lang="less">
.user-container {
  .info-box {
    height: 100px;
    background-color: #0098fa;
    display: flex;
    align-items: center;

    h3 {
      font-size: 20px;
      color: #fff;
      font-weight: normal;
    }

    .van-image {
      padding: 0 15px;
    }
  }

  .van-row {
    text-align: center;

    .van-col {
      padding: 10px 0;
      span {
        display: block;
        font-size: 14px;
      }
    }
  }
}
</style>
